<?php
/**
 * Created by PhpStorm.
 * User: yellow
 * Date: 2019/6/25
 * Time: 15:22
 */

namespace app\admin\logic;

use app\admin\logic\code\ErrorCode;
use app\common\model\Activity;
use app\vehicle\model\ActivityLog;
use app\common\model\User;
use app\vehicle\model\Volunteer;
class ActivityLogic extends BaseLogic
{

    private $ActivityModel;

    public function __construct()
    {
        parent::__construct();
        $this->ActivityModel = new Activity();
    }

    public function applyList($tempWhere, $nowPage){
        $where[] = ['aid', '=', $tempWhere['id']];
        $count = ActivityLog::where($where)->count();
        if ($count > 0){
            $articleList = ActivityLog::where($where)->order('id desc')->page($nowPage, $tempWhere['limit'])->select();
            foreach($articleList as $k=>$v){
                $user = User::where('id','eq',$v['user_id'])->find();
                $articleList[$k]['nickname'] = $user['nick_name'];
            }
            return ajaxArr(ErrorCode::$SUCCESS, 'success', $articleList, $count);
        } else{
            return ajaxArr(ErrorCode::$LISTNULL, '列表为空');
        }
    }

    public function ActivityList($tempWhere, $nowPage){
        if ($tempWhere['title']){
            $where[] = ['title', 'like', "%".$tempWhere['title']."%"];
        }
        $oid = $_SESSION['xm_travel']['admin_info']['oid'];
        $oid && $where[] = ['vid', '=', $oid];
        $count = $this->ActivityModel->where($where)->count();
        if ($count > 0){
            $ActivityList = $this->ActivityModel->where($where)->page($nowPage, self::$pageNum)->order('id desc')->select();
            foreach($ActivityList as $k=>$v){
                $shop_name = Volunteer::where('user_id','eq',$v['vid'])->value('realname');
                $ActivityList[$k]['shop_name'] = $shop_name?$shop_name:'后台系统';
            }
            return ajaxArr(ErrorCode::$SUCCESS, 'success', $ActivityList, $count);
        } else{
            return ajaxArr(ErrorCode::$LISTNULL, '列表为空');
        }
    }



    /**
     * 查找文章信息
     * @param $ActivityId
     * @return mixed
     */
    public function info($ActivityId){
        $info = $this->ActivityModel->get($ActivityId);
        if ($info){
            return ajaxArr(ErrorCode::$SUCCESS, 'success', $info);
        } else{
            return ajaxArr(ErrorCode::$INFONULL, '该文章不存在');
        }
    }

    /**
     * 文章逻辑操作
     * @param $data
     * @return mixed
     */
    public function handle($data){
        $act = $data['act'];
        unset($data['act']);
        $res = false;
        if(!$data['thumb']){
            unset($data['thumb']);
        }
        $data['use_time'] = strtotime($data['use_time']);
        $data['end_time'] = strtotime($data['end_time']);
        switch ($act){
            case 'add':
                $oid = $_SESSION['xm_travel']['admin_info']['oid'];
                $oid && $data['vid'] = $oid;
                $data['content'] = htmlspecialchars($data['content']);
                $res = $this->ActivityModel->allowField(true)->save($data);
                break;
            case 'edit':
                $res = $this->ActivityModel->allowField(true)->save($data, $data['id']);
                break;
            case 'del':
                $Activity = $this->ActivityModel->get($data['id']);
                $res = $Activity->delete();
                break;
            default:
                break;
        }
        if ($res){
            return ajaxArr(ErrorCode::$SUCCESS, '操作成功');
        } else{
            return ajaxArr(ErrorCode::$OPERATIONERROR, '操作失败');
        }
    }

}